﻿namespace MultipleMonitorVideo.ViewModels
{
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Linq;
    using System.Windows;
    using Caliburn.Micro;
    using Modules;
    using Utilities;
    using Screen = System.Windows.Forms.Screen;

    [Export(typeof(ControlPanelViewModel))]
    public class ControlPanelViewModel : ViewModel
    {
        [ImportingConstructor]
        public ControlPanelViewModel()
        {
            this.DisplayName = "FIRST Robotics Control Panel";
            
            IList<Module> modules = new List<Module>
            {
                new OverlayInfoModule(new OverlayInfoControlViewModel(), 0),
                new VideoModule(new VideoControlViewModel(), 1)
            };

            this.ModuleManager = new ModuleManager(modules);
            Screen mainScreen = ScreenManager.GetPrimaryScreen();
            Screen secondaryScreen = ScreenManager.GetSecondaryScreen() ?? mainScreen;
            ////this.ModuleManager.UpdateCurrentModule(typeof(OverlayInfoModule), secondaryScreen);
        }

        public ModuleManager ModuleManager
        {
            get;
            private set;
        }

        public IEnumerable<ViewModel> ModuleControls
        {
            get
            {
                return this.ModuleManager.Modules.Select(module => module.ControlViewModel);
            }
        }

        public void CloseAllWindows()
        {
            this.ModuleManager.DisableAllModules();
        }

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);

            Screen screen = ScreenManager.GetPrimaryScreen();
            Window window = view as Window;

            if (window == null)
            {
                return;
            }

            window.Top = screen.Bounds.Top;
            window.Left = screen.Bounds.Left;
            window.WindowState = WindowState.Maximized;
        }
    }
}
